#ifndef FRAME_EVENT_H_
#define FRAME_EVENT_H_
#pragma once
#include <QtGui>
#include "frame_head.h"

namespace MFC_namespace
{
	typedef enum MFCtype
	{
		invalid = 0, add = 1, del = 2, edit = 3, strategycom = 5
	}MFCtype;
}


typedef struct MFCData
{
	MFCData()
	{
		type = MFC_namespace::invalid;
		memset(&bar, 0, sizeof(BarInfo));
		timertype = TimerFire_namespace::invalid;
		varsize = 0;
		pvardata = NULL;
	}

	~MFCData()
	{
		type = MFC_namespace::invalid;
		memset(&bar, 0, sizeof(BarInfo));
		timertype = TimerFire_namespace::invalid;
		varsize = 0;
		if (pvardata)
		{
			delete pvardata;
			pvardata = NULL;
		}
	}


	MFC_namespace::MFCtype type;
	BarInfo bar;
	TimerFire_namespace::TimerFireType timertype;
	unsigned int varsize;
	char* pvardata;
}MFCData;


typedef struct ThreadCom
{
	Mq* pmq;
	HANDLE han;
	string hanid;
}ThreadCom;

typedef struct ThreadCom_Round
{
	ThreadCom_Round()
	{
		pmq = NULL;
		han[0] = NULL;
		han[1] = NULL;
	}

	Mq* pmq;
	HANDLE han[2];
	string hanid;
}ThreadCom_Round;


typedef enum FrameCom_Type
{
	mfctype = 1, tdtype = 2, timerfiretype = 3, terminatetype = 4 
}FrameCom_Type;

typedef struct FrameCom_Data
{
	FrameCom_Type type;
	unsigned int varsize;
	char* pvardata;
}FrameCom_Data;



class CFrameEvent: public QObject
{

	Q_OBJECT

public:

	// interface in level 1
	CFrameEvent(CStrategySys* pStrategy_sys);

	~CFrameEvent();

	void ReleaseFrameSemaAndSetData(FrameCom_Data* pFramecomData);
	
	void Frame();

	void OnQryInstrument(CThostFtdcInstrumentField *pInstrument, int reqid);

	void OnQryTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, int reqid);

	void OnQrytCommissionRate(CThostFtdcInstrumentCommissionRateField *pInstrumentCommissionRate, int reqid);

	CStrategySys* StrategySys();

private:
	//level 2, called by level 1 interface CFrameEvent();
	bool InitThreadTimer();

	bool DestroyThreadTimer();

	void ResetThreadTimer();

	bool InitThreadMFC();
	
	bool DestroyThreadMFC();

	void ResetThreadMFC();

	bool InitThreadTd();

	bool DestroyThreadTd();

	void ResetThreadTd();

	bool InitThreadTerminate();

	bool DestroyThreadTerminate();




	//levle 2, called by level 1 interface ReleaseFrameSemaAndSetData();
	void ReleaseTdSemAndSetData(TdData* pdata);

	void ReleaseTimerSemAndSetData(TimerData* pdata);

	void ReleaseMFCSemAndSetData(MFCData* pmfcdata);

	void ReleaseTerminateSem();

	//void ReleaseStrategycomSemAndSetData(StrategyComData* pdata);



	//levle 2, called by level 1 interface Frame();
	void Timer_Frame();

	void MFC_Frame();

	void Md_Frame(DWORD index);

	void Td_Frame();

	//void StrategyCom_Frame();

	


	//levle 3, called by level 2 function MFC_Frame();
	bool InitBar(BarInfo* pbar, char* puserdata);

	bool DestroyBar(BarInfo* pbar);

	bool EditBar(BarInfo* pbar, char* puserdata);




	//levle 4, called by level 3 function InitBar()/DestroyBar()/EditBar();
	bool InitBarInfo(BarInfo* pbar);

	bool DestroyBarInfo(BarInfo* pbar);

	bool InitBarBroad(BarInfo* pbar, HANDLE *phan);

	bool DestroyBarBroad(BarInfo* pbar);

	bool InitBarHandle(BarInfo* pbar, HANDLE* phan);

	bool DestroyBarHandle(BarInfo* pbar);

	bool InitBarStratety(BarInfo* pbar, char* puserdata);

	bool DestroyBarStrategy(BarInfo* pbar);

	bool EditBarStrategy(BarInfo* pbar, char* puserdata);

	bool InitSingaltoQt(BarInfo* pbar);

	bool DestroySingaltoQt(BarInfo* pbar);

public:
	vector<BarInfo*> m_vbarinfo;

	bool m_bInvalid;

signals:

	void SignalInit(BarSeries* pBarSer, BarData* pBar);

	void SignalDestroy(BarInfo* pbar);


private:
	
	//unsigned int m_tindex;

	//used to store md and td semaphore
	vector <HANDLE> m_vthreadhan;
	vector <string> m_vthreadhanname;

	//used to store broadcast for each instrument
	vector<BroadcastOpen*> m_vbroad;





	CStrategySys* m_strategy_sys;
	//map<string, OrderSysMapSeries*> m_mapmapser;
	//map<string, vector<BarSeries*>> m_mapuserbar;

	//used to store td mq
	ThreadCom m_tdcom;
	
	//used to store MFC related
	ThreadCom_Round m_mfccom;

	//used to store timer related
	ThreadCom_Round m_timercom;

	//used to store terminate related
	ThreadCom m_terminatecom;

	//used to copy from m_strategy_sys
	ThreadCom_Round m_strategycom;







/*******************************************static *******************************************************/
public:

	static vector<CFrameEvent*> vFrameThread;

private:
	static void AddInstance(CFrameEvent* f);

	static void RemoveInstance(CFrameEvent* f);
	

/*********************************************************************************************************/
};



#endif